

// global.globalKey = "123"
const jwt = require('../util/jwt')

module.exports = class users_dao extends require('../sql/user_sql'){
    /**
     * 登录
     * @param req
     * @param resp
     * @returns {Promise<void>}
     * @constructor
     */
    static async Login(req,resp){
        let body = req.body;
        let loginData = await this.LoginUser(body.username,body.password);
        // 获取到登录信息则登陆成功
        if (loginData.length != 0){
            let jwt_token = jwt.sign({
                id:loginData[0].id,
                username:loginData[0].username,
                email:loginData[0].email,
                head:loginData[0].icon,
                type:loginData[0].type
            },global.globalKey,60*60*24);
            resp.send({loginData,jwt_token})
        } else {
            resp.status(500).send('用户名账号或密码错误')
        }

    }

    /**
     * 根据token解析成用户信息
     * @param req
     * @param resp
     */
    static async getUserByToken(req,resp){
        let result = await jwt.verifysync(req.query.token,global.globalKey)
        resp.send(result)

    }

    /**
     * 根据用户类型获取用户信息（分页总数量与数据）
     */

    static async getUsersByTypePage(req,resp){
        console.log(req.query)
        let query = req.query
        let data = await this.getUsersByTypePageMod(query.type,query.currPage,query.pageNum)
        let total = await this.getUsersByTypePageTotal(query.type)
        resp.send({data,total:total[0].count})
    }

    /**
     * 获取总用户信息表
     */
    static async getUsersData(req,resp){
        console.log(req.query)
        let query = req.query
        let pageNum = query.pageNum;
        let currPage = query.currPage;
        // let username =query.inputText;
        let data = await this.getUsersDataMod(currPage,pageNum)
        let total = await this.getUsersDataTotal()
        resp.send({data,total:total[0].count})
    }


    /**
     * 用户删除
     * @param req
     * @param resp
     */
    static async delUserData(req,resp){
        let results = await this.delUserDataMod(req.query.u_id)
        resp.send(results)
    }

    /**
     * 用户编辑
     * @param req
     * @param resp
     */
    static async upUserData(req,resp){
        let body = req.body;
        let u_id = body.u_id;
        let username = body.username;
        let password = body.password;
        let nickname = body.nickname;
        let sex = body.sex;
        let homeaddress = body.homeaddress;
        let type = body.type;
        let email = body.email;
        let note = body.note;
        // let parkstatus = body.parkstatus;
        
        // let createtime = body.createtime;

        let results = await this.upUserDataMod(u_id,username,password,nickname,sex,homeaddress,type,email,note)
        resp.send(results)

    }

    /**
     * 用户新增
     * @param req
     * @param resp
     */
    static async addUserData(req,resp){
        let body = req.body;
        let id = body.id
        // let token = body.token
        let username = body.username;
        let password = body.password;
        let type = body.type;
        let email = body.email;
        let nickname = body.nickname;
        let sex = body.sex;
        let homeaddress = body.homeaddress;
        let note = body.note;
        // let createtime = body.createtime;

        // let verify = await jwt.verifysync(token, globalKey)
        // let u_id = verify.id;
        let data = await this.addUserDataMod(id,username,password,type,email,nickname,sex,homeaddress,note)
        resp.send(data)
    }

    /**
     * 密码修改
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async updatePwd(req,resp){
        let verify=await jwt.verifysync(req.body.token,globalKey)
        let id=verify.id;
        // let id = req.body.id;
        let oldPwd=req.body.oldPwd;
        let newPwd=req.body.newPwd;
        let results=await this.updatePwdSql(newPwd,id,oldPwd);
        if (results.changedRows==0){
            resp.send("修改失败,可能是旧密码错误或者新密码与旧密码相同")
        } else{
            resp.send("密码修改成功")
        }
    }

    /**
     * 根据id获取我的信息
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async getMyId(req,resp){
        let verify = await jwt.verifysync(req.body.token,globalKey)
        let id = verify.id;
        let data = await this.getMyIdSql(id)
        resp.send(data)
    }

    /**
     * 修改头像
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async upHead(req,resp){
        let results = await this.upHeadSql(req.head_imgUrl, req.u_id)
        resp.send(results)
    }


    /**
     * 修改用户车位状态
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async updateParkStatus(req,resp){
        let body = req.body;
        let u_id = body.u_id;
        let parkname = body.parkname;
        let parkstatus = body.parkstatus;
        let parkId = body.parkId;
        // let createtime = body.createtime;

        let results = await this.updateParkStatusSql(u_id,parkname,parkstatus,parkId)
        resp.send(results)

    }


    /**
     * 联表修改状态
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async upDatePark(req,resp){
        let body = req.body;
        let parkId = body.parkId;
        let u_id = body.u_id;
        let parkname = body.parkname;
        let parkstatus = body.parkstatus;

        let results = await this.upDateParkSql(parkId,parkstatus,parkname);
        results += await this.upParkSql(u_id,parkstatus);
        resp.send(results)

    }


    /**
     * 登录日志录入
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async addloginData(req,resp){
        let body = req.body;
        let username = body.username;
        let type = body.type;
        let email = body.email;
        // let createtime = body.createtime;
        let results = await this.addloginDataSql(username,type,email)
        resp.send(results)
    }


    /**
     * 登录日志查询
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async getLoginData(req,resp){
        console.log(req.query);
        let query = req.query;
        let username =query.username;
        let type = query.type;
        let startDate = query.startDate;
        let endDate = query.endDate;
        let pageNum = query.pageNum;
        let currPage = query.currPage;
        // let username =query.inputText;
        let data = await this.getLoginDataSql(username,type,startDate,endDate,currPage,pageNum)
        let total = await this.getLoginDataTotal(username,type,startDate,endDate)
        resp.send({data,total:total[0].count})
    }


    /**
     * 用户删除
     * @param req
     * @param resp
     */
    static async delLoginData(req,resp){
        let results = await this.delLoginDataSql(req.query.id)
        resp.send(results)
    }


    /**
     * 获取当月用户登录情况
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async getLoginMonth(req,resp){
        // var firstDay = new Date(nowYear,nowMonth,1)  // 本月开始时间
        // var LastDay = new Date(nowYear, nowMonth+1,0); // 本月结束时间
        let nowDate= this.getNowAndLastDate().nowMonth;
        let lastDate= this.getNowAndLastDate().lastMonth;
        let data= await this.getLoginMonthSql(nowDate,lastDate);
        resp.send(data)
    }

    /**
     * 用户新增量
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async getLogMonth(req,resp){
        // var firstDay = new Date(nowYear,nowMonth,1)  // 本月开始时间
        // var LastDay = new Date(nowYear, nowMonth+1,0); // 本月结束时间
        let nowDate= this.getNowAndLastDate().nowMonth;
        let lastDate= this.getNowAndLastDate().lastMonth;
        let data= await this.getLogMonthSql(nowDate,lastDate);
        resp.send(data)
    }


    /**
     * 获取用户模糊查询表
     * @param req
     * @param resp
     * @returns {Promise<void>}
     */
    static async getUserList(req,resp){
        let query = req.query;
        let username = query.username;
        let currPage = query.currPage;
        let pageNum = query.pageNum;
        let data = await this.getUserListSql(username,currPage,pageNum)
        let total = await this.getUserListTotal(username);
        resp.send({data,total:total[0].count})
    }
    
    




}